package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/***
 * 外部系统调用接口
 * @author chuck
 *
 */

import com.sl.au.entity.Account;
import com.sl.au.entity.AfterSaleTicket;
import com.sl.au.entity.AfterSaleTicketAdvice;
import com.sl.au.entity.AfterSaleTicketBack;
import com.sl.au.entity.EXDEViExdeBeforeContractWithSign;
import com.sl.au.entity.ExdeBeforeContract;
import com.sl.au.entity.ExdeBeforeContractCounterSign;
import com.sl.au.entity.ExdeBeforeContractDetailParts;
import com.sl.au.entity.ExdeBeforeContractDetails;
import com.sl.au.entity.ExdeBeforeContractNotices;
import com.sl.au.entity.ExdeBeforeContractSpecialClause;
import com.sl.au.entity.ExdeBeforeContractWGDetail;
import com.sl.au.entity.MyTask;
import com.sl.au.entity.Option;
import com.sl.au.service.AuditNodeService;
import com.sl.au.service.ExdeBeforeContractService;
@RestController

@RequestMapping(value = "/ui/crmout")
public class OutProjectController extends BaseController{
	
	@Autowired
	ExdeBeforeContractService exdeBeforeContractService;
	@Autowired
	AuditNodeService auditNodeService;
	
	/***
	 * 获取合同签订前审批合同合同标的物子表的子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractdetailparts/getpage/{beforeDetailId}", method = RequestMethod.GET)
	public Map<String, Object> getExdeBeforeContractDetailPartsPage(@PathVariable String beforeDetailId,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		//int start = Integer.parseInt(request.getParameter("start"));
		//int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractDetailParts> contracts = exdeBeforeContractService.getExdeBeforeContractDetailPartsByDetailId(0, 1000, beforeDetailId);
		//long count = exdeBeforeContractService.getExdeBeforeContractDetailPartsCountByDetailId(beforeDetailId);
		map.put("data", contracts);
		return map;
	}
	
	/***
	 * 获取会签待办列表二期
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "BeforeContractCounterSign/list", method = RequestMethod.GET)
	public Map<String, Object> getCounterSignList(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		//int start = Integer.parseInt(request.getParameter("start"));
		//int length = Integer.parseInt(request.getParameter("length"));
		String userId = this.getMyAccount().getUser().getId();
		//String userId = "8d2d060e-b2cf-4b7f-ab2f-4cd5012bd915";
		List<EXDEViExdeBeforeContractWithSign> tasks = exdeBeforeContractService.getEXDEViExdeBeforeContractWithSignByUserId(userId);
		//int count = tasks.size();
		map.put("data", tasks);
		//map.put("draw", request.getParameter("draw"));
		//map.put("recordsFiltered", count);
		//map.put("recordsTotal", count);
		return map;
	}
	/***
	 * 获取会签待办列表crm
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "BeforeContractCounterSign/listCrm", method = RequestMethod.GET)
	public Map<String, Object> getCounterSignListCrm(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		//String userId = this.getMyAccount().getUser().getId();
		//String userId = "8d2d060e-b2cf-4b7f-ab2f-4cd5012bd915";
		List<EXDEViExdeBeforeContractWithSign> tasks = exdeBeforeContractService.getEXDEViExdeBeforeContractWithSignByUserId(userId);
		int count = tasks.size();
		map.put("data", tasks);
		//map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	
	/***
	 * 通过会签审批填写意见
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "BeforeContractCounterSign/{taskId}", method = RequestMethod.GET)
	public Map<String, Object> submitCounterSign(@PathVariable String taskId,HttpServletRequest request) {
		String cAuditMemo = request.getParameter("auditAdvice");
		Map<String, Object> map = new HashMap<String, Object>();
		String userId = this.getMyAccount().getUser().getId();
		String beforeId= this.exdeBeforeContractService.submitExdeBeforeContractCounterSign(taskId, userId, cAuditMemo);
		//判断会签是否完成改变签订前要点审批主表状态
		this.exdeBeforeContractService.updateExdeBeforeContractCounterSignFinishState(beforeId);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * 签订前要点审批外部查看和会签页面JSON
	 * 
	 * @return
	 */
	@RequestMapping(value = "/audit-view/{beforeId}/json", method = RequestMethod.GET)
	public Map<String, Object> aditorViewJSON(@PathVariable String beforeId) {
		Map<String, Object> map = new HashMap<>();
		//签订前要点主表
		ExdeBeforeContract exdeBeforeContract = exdeBeforeContractService.getBeforeContract(beforeId);
		//获取会签子表
		List<ExdeBeforeContractCounterSign> exdeBeforeContractCounterSign= exdeBeforeContractService.getBeforeContractsHQ(0, 1000, beforeId);
		//获取产品子表
		List<ExdeBeforeContractDetails> exdeBeforeContractDetails = exdeBeforeContractService.getExdeBeforeContractDetailsByBeforeId(0, 1000, beforeId);
		//其它注意条款子表
		List<ExdeBeforeContractNotices> exdeBeforeContractNotices = exdeBeforeContractService.getExdeBeforeContractNoticesByBeforeId(0, 1000, beforeId);
		//特殊条款子表
		List<ExdeBeforeContractSpecialClause> exdeBeforeContractSpecialClause = exdeBeforeContractService.getExdeBeforeContractSpecialClauseByBeforeId(0, 1000, beforeId);
		//制定外购件子表
		List<ExdeBeforeContractWGDetail> exdeBeforeContractWGDetail = exdeBeforeContractService.getExdeBeforeContractWGDetailByBeforeId(0, 1000, beforeId);
		map.put("exdeBeforeContract", exdeBeforeContract);
		map.put("exdeBeforeContractCounterSign", exdeBeforeContractCounterSign);
		map.put("exdeBeforeContractDetails", exdeBeforeContractDetails);
		map.put("exdeBeforeContractNotices", exdeBeforeContractNotices);
		map.put("exdeBeforeContractSpecialClause", exdeBeforeContractSpecialClause);
		map.put("exdeBeforeContractWGDetail", exdeBeforeContractWGDetail);
		//map.put("taskId", taskId);
		
		map.put("auditNodes", auditNodeService.getAll(exdeBeforeContract.getProcessInstId(), beforeId));
		return map;
	}
}
